package top.kmtec.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.kmtec.common.BaseResponse;
import top.kmtec.common.ErrorCode;
import top.kmtec.common.ResultUtils;
import top.kmtec.constant.UserConstant;
import top.kmtec.exception.BusinessException;
import top.kmtec.model.dto.UserDTO;
import top.kmtec.model.entity.User;
import top.kmtec.service.UserService;


import java.util.List;

/**
 * @Author FengHuan Wang
 * @Date 2025/2/4 9:15
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册接口
     * @param userDTO
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value = "用户注册接口")
    public BaseResponse<Long> userRegister(@RequestBody UserDTO userDTO) {
        if (userDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userDTO.getUserAccount();
        String password = userDTO.getPassword();
        String checkPassword = userDTO.getCheckPassword();
        String code = userDTO.getCode();
        if (StringUtils.isAllBlank(userAccount, password, checkPassword, code)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long register = userService.userRegister(userAccount, password, checkPassword, code);
        return ResultUtils.sucess(register);
    }

    /**
     * 用户登录接口
     * @param userDTO
     * @param request
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录接口")
    public BaseResponse<User> userLogin(@RequestBody UserDTO userDTO, HttpServletRequest request) {
        if (userDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userDTO.getUserAccount();
        String password = userDTO.getPassword();
        if (StringUtils.isAllBlank(userAccount, password)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.userLogin(userAccount, password, request);
        return ResultUtils.sucess(user);
    }

    /**
     * 用户查询接口
     * @param username
     * @return
     */
    @GetMapping("/search")
    public BaseResponse<List<User>> searchUser (String username, HttpServletRequest request) {
        //仅管理员课查询
        Object attribute = request.getSession().getAttribute(UserConstant.USER_LOGIN_STATUS);
        User user = (User) attribute;
        if (user == null|| user.getUserRole() != UserConstant.ADMIN_ROLE) {
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNoneBlank(username)) {
            queryWrapper.like("username", username);
        }
        List<User> list = userService.list(queryWrapper);
        return ResultUtils.sucess(list);
    }

    /**
     * 删除用户接口
     * @param id
     * @return
     */
    @DeleteMapping("/deluser")
    public BaseResponse<Boolean> delUser (Long id, HttpServletRequest request) {
        Object attribute = request.getSession().getAttribute(UserConstant.USER_LOGIN_STATUS);
        User user = (User) attribute;
        if (user == null|| user.getUserRole() != UserConstant.ADMIN_ROLE) {
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        if (id <= 0) {
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        boolean ret = userService.removeById(id);
        return ResultUtils.sucess(ret);
    }

    /**
     * 用户注销
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public BaseResponse<Integer> userLogout(HttpServletRequest request){
        if (request == null){
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        Integer logout = userService.userLogout(request);
        return ResultUtils.sucess(logout);
    }
}
